package com.song.tradition.page.main

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.tween

import androidx.compose.animation.*
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.navigation.NavType
import com.google.accompanist.navigation.animation.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.navArgument
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import com.song.tradition.entity.bookDataList
import com.song.tradition.entity.grassData
import com.song.tradition.entity.personDataList
import com.song.tradition.entity.storyData
import com.song.tradition.page.about.AboutScreen
import com.song.tradition.page.book.BookScreen
import com.song.tradition.page.components.BottomNavBarView
import com.song.tradition.page.components.CustomTopAppBar
import com.song.tradition.page.components.DrawerScreen
import com.song.tradition.page.components.ScreenRoute
import com.song.tradition.page.details.BookDetailScreen
import com.song.tradition.page.details.GrassDetailScreen
import com.song.tradition.page.details.OriginScreen
import com.song.tradition.page.details.PersonDetailScreen
import com.song.tradition.page.details.StoryDetailScreen
import com.song.tradition.page.history.HistoryScreen
import com.song.tradition.page.home.HomeScreen
import com.song.tradition.page.search.SearchScreen
import com.song.tradition.page.webview.WebViewScreen
import com.song.tradition.ui.theme.customBgColor
import kotlinx.coroutines.launch


@OptIn(ExperimentalMaterial3Api::class, ExperimentalAnimationApi::class)
@Composable
fun MainScreen() {
    val navController = rememberAnimatedNavController()
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentDestination = navBackStackEntry?.destination
    val scope = rememberCoroutineScope()
    val drawerState = rememberDrawerState(DrawerValue.Closed)

    ModalNavigationDrawer(
        drawerContent = {
            DrawerScreen(navController, drawerState = drawerState)
        }, drawerState = drawerState
    ) {
        Scaffold(
            modifier = Modifier
                .navigationBarsPadding()
                .background(customBgColor),

            topBar = {
                when (currentDestination?.route) {
                    ScreenRoute.HomeScreen.route -> {
                        CustomTopAppBar(navClick = {
                            scope.launch {
                                drawerState.apply {
                                    if (isClosed) open() else close()
                                }
                            }

                        })
                    }

                    ScreenRoute.BookScreen.route -> {
                        CustomTopAppBar(navClick = {
                            scope.launch {
                                drawerState.apply {
                                    if (isClosed) open() else close()
                                }
                            }

                        })
                    }

                    ScreenRoute.HistoryScreen.route -> {
                        CustomTopAppBar(navClick = {
                            scope.launch {
                                drawerState.apply {
                                    if (isClosed) open() else close()
                                }
                            }

                        })
                    }
                }
            },
//        底部导航栏
            bottomBar = {

//            加判断，如果不是这三个页面，就隐藏
                when (currentDestination?.route) {
                    ScreenRoute.HomeScreen.route -> {
                        BottomNavBarView(navController = navController)
                    }

                    ScreenRoute.BookScreen.route -> {
                        BottomNavBarView(navController = navController)
                    }

                    ScreenRoute.HistoryScreen.route -> {
                        BottomNavBarView(navController = navController)
                    }

                }
            },

            ) {
            Surface(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(
                        bottom = it.calculateBottomPadding(),
                        top = it.calculateTopPadding(),
                    )
            ) {
                AnimatedNavHost(
                    navController = navController,
                    startDestination = ScreenRoute.HomeScreen.route,
                ) {
                    composable(route = ScreenRoute.HomeScreen.route) {
                        HomeScreen(
                            navController = navController,
                        )
                    }

                    composable(route = ScreenRoute.BookScreen.route) {
                        BookScreen(
                            navController = navController
                        )
                    }

                    composable(route = ScreenRoute.HistoryScreen.route) {
                        HistoryScreen(
                            navController = navController
                        )
                    }
                    composable(route = ScreenRoute.AboutScreen.route,
                        enterTransition = {
                            slideInHorizontally(animationSpec = tween(200))
                        },
                        exitTransition = {
                            fadeOut(animationSpec = tween(50, easing = LinearOutSlowInEasing))
                        }) {
                        AboutScreen(
                            navController = navController
                        )
                    }

                    composable(route = ScreenRoute.OriginScreen.route,
                        enterTransition = {
                            slideInHorizontally(animationSpec = tween(200))
                        },
                        exitTransition = {
                            fadeOut(animationSpec = tween(50, easing = LinearOutSlowInEasing))
                        }) {
                        OriginScreen(
                            navController = navController
                        )
                    }

                    composable(route = ScreenRoute.PersonDetailScreen.route + "/{id}",
                        arguments = listOf(
                            navArgument("id") {
                                type = NavType.IntType
                            }
                        ),
                        enterTransition = {
                            slideInHorizontally(animationSpec = tween(200))
                        },
                        exitTransition = {
                            slideOutHorizontally(animationSpec = tween(200, easing = LinearOutSlowInEasing))
                        }) {entity->
                        val id = entity.arguments?.getInt("id")
                        PersonDetailScreen(
                            navController = navController,
                            personDataList.get(id!!)
                        )
                    }


                    composable(route = ScreenRoute.GrassDetailScreen.route + "/{id}",
                        arguments = listOf(
                            navArgument("id") {
                                type = NavType.IntType
                            }
                        ),
                        enterTransition = {
                            slideInHorizontally(animationSpec = tween(200))
                        },
                        exitTransition = {
                            fadeOut(animationSpec = tween(50, easing = LinearOutSlowInEasing))
                        }) { entity ->
                        val id = entity.arguments?.getInt("id")
                        GrassDetailScreen(
                            navController = navController,
                            data = grassData[id!!]
                        )
                    }
                    composable(route = ScreenRoute.BookDetailScreen.route + "/{id}",
                        arguments = listOf(
                            navArgument("id") {
                                type = NavType.IntType
                            }
                        ),
                        enterTransition = {
                            slideInHorizontally(animationSpec = tween(200))
                        },
                        exitTransition = {
                            fadeOut(animationSpec = tween(50, easing = LinearOutSlowInEasing))
                        }) { entity ->
                        val id = entity.arguments?.getInt("id")
                        BookDetailScreen(
                            navController = navController,
                            data = bookDataList[id!!]
                        )
                    }

                    composable(route = ScreenRoute.StoryDetailScreen.route + "/{id}",
                        arguments = listOf(
                            navArgument("id") {
                                type = NavType.IntType
                            }
                        ),
                        enterTransition = {
                            slideInHorizontally(animationSpec = tween(200))
                        },
                        exitTransition = {
                            fadeOut(animationSpec = tween(50, easing = LinearOutSlowInEasing))
                        }) { entity ->
                        val id = entity.arguments?.getInt("id")
                        StoryDetailScreen(
                            navController = navController,
                            data = storyData[id!!]
                        )
                    }
                    
                    composable(route=ScreenRoute.SearchScreen.route){
                        SearchScreen(navController = navController)
                    }
                }
            }

        }

    }
}




